import 'package:dujitang/global.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:dujitang/api.dart';
import 'package:dujitang/calendar_modal.dart';
import 'package:dujitang/page.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '喂你毒鸡汤',
      theme: ThemeData(
        primarySwatch: Colors.brown,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: '喂你毒鸡汤'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var _data = null;

  @override
  void initState() {
    getData(DateTime.now());
    super.initState();
  }

  getData(date) {
    var str = ['一', '二', '三', '四', '五', '六', '日'];
    var strEn = [
      'Monday',
      'Tuesday',
      'Wednesday',
      'Thursday',
      'Friday',
      'Saturday',
      'Sunday'
    ];
    var _date = DateFormat('yyyy年MM月dd日').format(date);
    var _day = date.day;
    var _weekday = str[date.weekday - 1];
    var _weekdayEn = strEn[date.weekday - 1];
    getDujitang().then((res) {
      print(res);
      setState(() {
        _data = {
          'text': res['text'].split('，'),
          'day': _day,
          'date': _date,
          'weekday': _weekday,
          'weekdayEn': _weekdayEn,
        };
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(
          widget.title,
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 12,
            fontWeight: FontWeight.bold,
          ),
        ),
        backgroundColor: Global.backgroundColor,
        elevation: 0,
        actions: <Widget>[
          IconButton(
            icon: Icon(
              Icons.date_range,
              color: Global.fontColor,
              size: 19,
            ),
            tooltip: '日历',
            onPressed: () {
              CalendarModal.show(
                context: context,
                date: DateTime.now(),
                onChange: (date) {
                  getData(date);
                },
              );
            },
          )
        ],
      ),
      body: Container(
        height: MediaQuery.of(context).size.height,
        color: Global.backgroundColor,
        child: SingleChildScrollView(
          child: ContentPage(
              data: _data,
              onRefresh: () {
                getDujitang().then((res) {
                  print(res);
                  setState(() {
                    _data['text'] = res['text'].split('，');
                  });
                });
              }),
        ),
      ),
    );
  }
}
